package javolution.io;

import java.io.CharConversionException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import javolution.lang.Reusable;
import okio.Utf8;
import org.apache.commons.net.bsd.RCommandClient;

/* loaded from: classes2.dex */
public final class UTF8StreamReader extends Reader implements Reusable {
    private final byte[] _bytes;
    private int _code;
    private int _end;
    private InputStream _inputStream;
    private int _moreBytes;
    private int _start;

    public UTF8StreamReader() {
        this._bytes = new byte[2048];
    }

    public UTF8StreamReader(int i) {
        this._bytes = new byte[i];
    }

    private int read2() throws IOException {
        int i;
        int i2;
        int i3 = this._start;
        if (i3 >= this._end) {
            InputStream inputStream = this._inputStream;
            if (inputStream != null) {
                this._start = 0;
                byte[] bArr = this._bytes;
                this._end = inputStream.read(bArr, 0, bArr.length);
                if (this._end > 0) {
                    return read2();
                }
                if (this._moreBytes == 0) {
                    return -1;
                }
                throw new CharConversionException("Unexpected end of stream");
            }
            throw new IOException("No input stream or stream closed");
        }
        byte[] bArr2 = this._bytes;
        this._start = i3 + 1;
        byte b = bArr2[i3];
        if (b < 0 || this._moreBytes != 0) {
            if ((b & 192) == 128 && (i2 = this._moreBytes) != 0) {
                this._code = (b & Utf8.REPLACEMENT_BYTE) | (this._code << 6);
                int i4 = i2 - 1;
                this._moreBytes = i4;
                return i4 == 0 ? this._code : read2();
            }
            if ((b & 224) == 192 && this._moreBytes == 0) {
                this._code = b & 31;
                this._moreBytes = 1;
            } else {
                if ((b & 240) == 224 && this._moreBytes == 0) {
                    this._code = b & 15;
                    i = 2;
                } else if ((b & 248) == 240 && this._moreBytes == 0) {
                    this._code = b & 7;
                    this._moreBytes = 3;
                } else if ((b & 252) == 248 && this._moreBytes == 0) {
                    this._code = b & 3;
                    i = 4;
                } else if ((b & 254) != 252 || this._moreBytes != 0) {
                    throw new CharConversionException("Invalid UTF-8 Encoding");
                } else {
                    this._code = b & 1;
                    i = 5;
                }
                this._moreBytes = i;
            }
            return read2();
        }
        return b;
    }

    @Override // java.io.Reader, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        InputStream inputStream = this._inputStream;
        if (inputStream != null) {
            inputStream.close();
            reset();
        }
    }

    @Override // java.io.Reader
    public int read() throws IOException {
        byte[] bArr = this._bytes;
        int i = this._start;
        byte b = bArr[i];
        if (b >= 0) {
            this._start = i + 1;
            if (i < this._end) {
                return b;
            }
        }
        return read2();
    }

    @Override // java.io.Reader
    public int read(char[] cArr, int i, int i2) throws IOException {
        InputStream inputStream = this._inputStream;
        if (inputStream != null) {
            if (this._start >= this._end) {
                this._start = 0;
                byte[] bArr = this._bytes;
                this._end = inputStream.read(bArr, 0, bArr.length);
                int i3 = this._end;
                if (i3 <= 0) {
                    return i3;
                }
            }
            int i4 = i + i2;
            int i5 = i;
            while (i5 < i4) {
                byte[] bArr2 = this._bytes;
                int i6 = this._start;
                byte b = bArr2[i6];
                if (b >= 0) {
                    int i7 = i6 + 1;
                    this._start = i7;
                    if (i7 < this._end) {
                        cArr[i5] = (char) b;
                        i5++;
                    }
                }
                if (b >= 0) {
                    cArr[i5] = (char) b;
                    return (i5 + 1) - i;
                }
                if (i5 < i4 - 1) {
                    int read2 = read2();
                    if (read2 < 65536) {
                        cArr[i5] = (char) read2;
                        i5++;
                    } else if (read2 > 1114111) {
                        throw new CharConversionException("Cannot convert U+" + Integer.toHexString(read2) + " to char (code greater than U+10FFFF)");
                    } else {
                        int i8 = i5 + 1;
                        int i9 = read2 - 65536;
                        cArr[i5] = (char) ((i9 >> 10) + 55296);
                        i5 = i8 + 1;
                        cArr[i8] = (char) ((i9 & RCommandClient.MAX_CLIENT_PORT) + Utf8.LOG_SURROGATE_HEADER);
                    }
                    if (this._start < this._end) {
                    }
                }
                return i5 - i;
            }
            return i2;
        }
        throw new IOException("No input stream or stream closed");
    }

    public void read(Appendable appendable) throws IOException {
        if (this._inputStream == null) {
            throw new IOException("No input stream or stream closed");
        }
        while (true) {
            if (this._start >= this._end) {
                this._start = 0;
                InputStream inputStream = this._inputStream;
                byte[] bArr = this._bytes;
                this._end = inputStream.read(bArr, 0, bArr.length);
                if (this._end <= 0) {
                    return;
                }
            }
            byte b = this._bytes[this._start];
            if (b >= 0) {
                appendable.append((char) b);
                this._start++;
            } else {
                int read2 = read2();
                if (read2 >= 65536) {
                    if (read2 > 1114111) {
                        throw new CharConversionException("Cannot convert U+" + Integer.toHexString(read2) + " to char (code greater than U+10FFFF)");
                    }
                    int i = read2 - 65536;
                    appendable.append((char) ((i >> 10) + 55296));
                    read2 = (i & RCommandClient.MAX_CLIENT_PORT) + Utf8.LOG_SURROGATE_HEADER;
                }
                appendable.append((char) read2);
            }
        }
    }

    @Override // java.io.Reader
    public boolean ready() throws IOException {
        InputStream inputStream = this._inputStream;
        if (inputStream != null) {
            return this._end - this._start > 0 || inputStream.available() != 0;
        }
        throw new IOException("Stream closed");
    }

    @Override // java.io.Reader, javolution.lang.Reusable
    public void reset() {
        this._code = 0;
        this._end = 0;
        this._inputStream = null;
        this._moreBytes = 0;
        this._start = 0;
    }

    public UTF8StreamReader setInput(InputStream inputStream) {
        if (this._inputStream == null) {
            this._inputStream = inputStream;
            return this;
        }
        throw new IllegalStateException("Reader not closed or reset");
    }

    public UTF8StreamReader setInputStream(InputStream inputStream) {
        return setInput(inputStream);
    }
}
